package image

import (
	"context"
	"time"

	"go-weixin/app/guitar/api/internal/svc"
	"go-weixin/app/guitar/api/internal/types"
	"go-weixin/app/guitar/rpc/pb/proto/guitar"

	"github.com/zeromicro/go-zero/core/logx"
)

type GuitarImageListLogic struct {
	logx.Logger
	ctx    context.Context
	svcCtx *svc.ServiceContext
}

func NewGuitarImageListLogic(ctx context.Context, svcCtx *svc.ServiceContext) *GuitarImageListLogic {
	return &GuitarImageListLogic{
		Logger: logx.WithContext(ctx),
		ctx:    ctx,
		svcCtx: svcCtx,
	}
}

func (l *GuitarImageListLogic) GuitarImageList(req *types.GuitarImageListReq) (resp *types.GuitarImageListResp, err error) {
	ctx, cannFunc := context.WithTimeout(context.Background(), time.Second*5)
	defer cannFunc()
	res, err := l.svcCtx.GuitarRPC.GuitarImageList(ctx, &guitar.GuitarImageListReq{
		Page: req.Page,
	})
	var imageList []types.GuitarImage
	if len(res.ImageList) > 0 {
		for _, m := range res.ImageList {
			imageList = append(imageList, types.GuitarImage{
				Id:       m.Id,
				GuitarId: m.GuitarId,
				Url:      m.Url,
			})
		}
	}
	resp = new(types.GuitarImageListResp)
	resp.ImageList = imageList
	return
}
